package com.oyjd.beauty.business.recommand.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oyjd.beauty.bean.po.Recommand;
import com.oyjd.beauty.business.goods.vo.GoodsVOEx;
import com.oyjd.beauty.business.recommand.dao.recommandDao;
import com.oyjd.beauty.business.recommand.service.recommandService;
import com.oyjd.beauty.fw.action.Page;

/**
 * 
 * 店长推荐业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class recommandServiceImpl implements recommandService {
	private recommandDao dao;

	@Autowired
	public void setDao(recommandDao dao) {
		this.dao = dao;
	}

	/**
	 * 交换资料的排序位置
	 * 
	 * @param id1
	 *            Id1商品ID
	 * @param id2
	 *            Id2商品ID
	 */
	@Transactional(readOnly = false)
	public void exchangePos(String id1, String id2) {
		String hql = "from " + Recommand.class.getName() + " where goodsId=?";
		Recommand o1 = (Recommand) dao.queryByHql(hql, new Object[] { id1 }).get(0);
		Recommand o2 = (Recommand) dao.queryByHql(hql, new Object[] { id2 }).get(0);
		int n = o1.getOrderNo();
		o1.setOrderNo(o2.getOrderNo());
		o2.setOrderNo(n);
		dao.update(o1);
		dao.update(o2);
	}

	/**
	 * 将资料的排至首位
	 * 
	 * @param id
	 *            Id商品ID
	 */
	@Transactional(readOnly = false)
	public void moveFirst(String id) {
		//
		int max = dao.getMaxOrderNo();
		//
		String hql = "from " + Recommand.class.getName() + " where goodsId=?";
		Recommand ts = (Recommand) dao.queryByHql(hql, new Object[] { id }).get(0);
		ts.setOrderNo(max + 1);
		dao.update(ts);
	}

	/**
	 * 新增类型排序元素
	 * 
	 * @param ids商品ID
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void addOrder(String[] ids, String user) {
		int max = dao.getMaxOrderNo();
		int n = max + ids.length;
		Date date = new Date();
		for (int i = 0; i < ids.length; i++) {
			Recommand r = new Recommand();
			r.setGoodsId(ids[i]);
			r.setOrderNo(n - i);
			r.setCreator(user);
			r.setCreateTime(date);
			r.setLastEditor(user);
			r.setLastUpdateTime(date);
			r.setStatus(0);
			dao.create(r);
		}
	}

	/**
	 * 删除类型排序元素
	 * 
	 * @param ids
	 *            商品ID
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void removeOrder(String[] ids, String user) {
		String hql = "delete from " + Recommand.class.getName() + " where (";
		List<String> para = new ArrayList<String>();
		for (int i = 0; i < ids.length; i++) {
			hql += i == 0 ? "" : " or ";
			hql += "goodsId=?";
			para.add(ids[i]);
		}
		hql += ")";
		dao.executeHql(hql, para.toArray(new Object[0]));
	}

	/**
	 * 获取分类排序的列表
	 * 
	 * @param page
	 * 
	 * @return
	 */
	public Page<GoodsVOEx> getList(Page<GoodsVOEx> page) {
		return dao.getList(page);
	}

	/**
	 * 获取差异的列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<GoodsVOEx> getLeftList(Page<GoodsVOEx> page) {
		return dao.getLeftList(page);
	}

}
